<?php

namespace app\admin\controller;

use app\admin\model\Category as Categories;
use app\admin\model\Image;
use think\Request;

class Category extends Base
{
    public function category_list()
    {

        $categories = Categories::with(['image'])->paginate(2);

        return view('category/category-list', [
            'categories' => $categories
        ]);
    }

    public function category_add(Request $request)
    {
        if ($request->method() == 'POST') {
            $data = $request->param();

            $cate = Categories::add($data);

            if ($cate) {

                $this->success('添加成功');
            }

            $this->error('添加失败');


        }

        return view('category/category-add');
    }

    public function category_edit(Request $request,$id)
    {
        if ($request->method() == 'POST') {
            $data = $request->param();


            if ($cate = Categories::find($data['id'])){

                Categories::where('id',$data['id'])->update(['name'=>$data['name'],'description'=>$data['description']]);

                Image::where('id',$cate->topic_img_id)->update(['url'=>$data['url']]);

                $this->success('编辑成功');

            }else{

                $this->error('编辑失败');

            }
        }

        $cate = Categories::with(['image'])->find($id);

        return view('category/category-edit',[
            'cate'=>$cate
        ]);
    }

    public function category_delete()
    {

        if (Categories::cate_del($_POST['ids'])){

            $this->success('删除成功');

        }else{

            $this->error('删除失败');

        }


    }
}
